package v3.chapter2.synBlockString2; 

public class Run {

	public static void main(String[] args) {
		Service service=new Service();
		MyThread1 a=new MyThread1(service);
		a.setName("A");
		a.start();
		
		MyThread2 b=new MyThread2(service);
		b.setName("B");
		b.start();

	}

}


class MyThread1 extends Thread {

	private Service service;

	public MyThread1(Service service) {
		super();
		this.service = service;
	}

	@Override
	public void run() {
		service.a();
		
	}

}

class MyThread2 extends Thread {

	private Service service;

	public MyThread2(Service service) {
		super();
		this.service = service;
	}

	@Override
	public void run() {
		service.b();
		
	}

} 

class Service{ 
	private String anyString=new String();
	
	public void a() {
		try { 
			synchronized(anyString) {
				System.out.println("a begin"); 
				Thread.sleep(3000);
				System.out.println(" a end");
			}
		}catch(InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	synchronized public void b() {
		System.out.println("b begin");
		System.out.println(" b end");
	}
}
